<template>
  <view class="notice"
  >
    <uv-tags :text="dictData.text||''" :plain="plain" :type="dictData.class" :size="properties.size?'mini':properties.size"></uv-tags>
  </view>
</template>
<script>

import {getDicts} from "@/api/system/dict";

export default {
  name: "Dict",
  props: {
    properties: {
      type: Object,
      default: function () {
        return {
          dictType: "",
          dictValue: 0,
          dictName: '待接单',
          plain: false,
          size: 'mini'
        }
      }
    },
    plain :{
      type: Boolean,
      default: function (){
        return true
      }
    }
  },
  data() {
    return {
      dictData: {
        text: "",
        class: ""
      }
    }
  },
  watch: {
    properties: {
      handler(n, o) {
        this.getDicData(n.dictType, n.dictValue);
      },
      immediate: true, // 正确设置
      deep: true // 如果需要深度监听对象内部的变化
    }
  },
  methods: {
    async getDicData(dictType, dictValue) {
      let data = this.$store.state.app.dicData;
      if (data !==undefined && data !== null) {
        if (typeof data === 'string') {
          data = JSON.parse(data)
        }
        // console.log("dictData", data)
        // console.log("dictData dictType dictValue", data, dictType, dictValue)
        let dictDataV = data[dictType];
        if (dictDataV !== undefined) {
          let dictData = dictDataV[dictValue + ''];
          this.dictData = dictData;
        } else {
          let map = new Map();
         await getDicts(dictType).then(res => {
            let datas = res.data;
            let mapV = {};
            for (const resKey in datas) {
              let item = datas[resKey];
              mapV[item.dictValue] = {
                value: item.dictValue,
                text: item.dictLabel,
                class: item.listClass
              }

              if (item.dictValue === dictValue){
                  this.dictData.text = item.dictLabel
                  this.dictData.class = item.listClass
              }

            }
            map.set(dictType, mapV);
            console.log('mapV', map)
            this.$store.commit('DICT_DATA', map);

          })
        }
      } else {
        let map = new Map();
        await getDicts(dictType).then(res => {
          let datas = res.data;
          let mapV = {};
          for (const resKey in datas) {
            let item = datas[resKey];
            mapV[item.dictValue] = {
              value: item.dictValue,
              text: item.dictLabel,
              class: item.listClass
            }
            if (item.dictValue === dictValue){
              this.dictData.text = item.dictLabel
              this.dictData.class = item.listClass
            }
          }
          map.set(dictType, mapV);
          console.log('mapV', map)
          this.$store.commit('DICT_DATA', map);
        })
      }
    }
  },
  created() {
    this.getDicData(this.properties.dictType, this.properties.dictValue)
  }
}
</script>

<style scoped lang="scss">

</style>
